//
//  STDatabaseManager.m
//  STBasicsApp
//
//  Created by jerry on 2019/5/2.
//  Copyright © 2019 jerry. All rights reserved.
//

#import "STDatabaseManager.h"
#define ST_MAINDB_NAME @"MAIN_DB"

@interface STDatabaseManager()

@property(nonatomic,strong)LKDBHelper * dbHelper;
@property(nonatomic,copy)  NSString   * dbFilePath;

@end

@implementation STDatabaseManager

static STDatabaseManager * instance = nil;

+ (instancetype)sharedDatabaseManager{
    return [[self alloc] init];
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [super allocWithZone:zone];
    });
    return instance;
}

///初始化
- (instancetype)init {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [super init];
        //初始化设置
        [self createTables];
    });
    return instance;
}

///创建数据表
- (void)createTables{
//    BOOL UserResult = [self.dbHelper getTableCreatedWithClass:[STComUserIModel class]];
//    if (!UserResult) {
//        [self.dbHelper createTableWithModelClass:[STComUserIModel class]];
//    }

}

- (LKDBHelper *)getLKDBHelper{
    return self.dbHelper;
}

#pragma mark - 表操作部分
///创建表
- (BOOL)createTableWithModelClass:(Class)modelClass{
    if (!modelClass) {
        return NO;
    }
    return [self.dbHelper createTableWithModelClass:modelClass];
}
///删除表
- (void)dropAllTable{
    [self.dbHelper dropAllTable];
}
- (BOOL)dropTableWithClass:(Class)modelClass{
    if (!modelClass) {
        return NO;
    }
    return [self.dbHelper dropTableWithClass:modelClass];
}
- (BOOL)dropTableWithTableName:(NSString *)tableName{
    if (!(tableName && ![tableName isEqualToString:@""])) {
        return NO;
    }
    return [self.dbHelper dropTableWithTableName:tableName];
}
#pragma mark - 数据操作部分
///插入数据
- (BOOL)insertToDB:(NSObject *)model{
    if (!model) {
        return NO;
    }
    return [self.dbHelper insertToDB:model];
}
- (void)insertToDB:(NSObject *)model callback:(void (^)(BOOL result))block{
    if (!model) {
        return;
    }
    [self.dbHelper insertToDB:model callback:block];
}
- (BOOL)insertWhenNotExists:(NSObject *)model{
    if (!model) {
        return NO;
    }
    return [self.dbHelper insertWhenNotExists:model];
}
- (void)insertWhenNotExists:(NSObject *)model callback:(void (^)(BOOL result))block{
    if (!model) {
        return;
    }
    [self.dbHelper insertWhenNotExists:model callback:block];
}

///修改数据
- (BOOL)updateToDB:(NSObject *)model where:(id)where{
    if (!model) {
        return NO;
    }
    return [self.dbHelper updateToDB:model where:where];
}
- (void)updateToDB:(NSObject *)model where:(id)where callback:(void (^)(BOOL result))block{
    if (!model) {
        return;
    }
    [self.dbHelper updateToDB:model where:where callback:block];
}

///删除数据
- (BOOL)deleteToDB:(NSObject *)model{
    if (!model) {
        return NO;
    }
    return [self.dbHelper deleteToDB:model];
}
- (void)deleteToDB:(NSObject *)model callback:(void (^)(BOOL result))block{
    if (!model) {
        return;
    }
    [self.dbHelper deleteToDB:model callback:block];
}

///查询数据
- (NSMutableArray *)queryAllDatas:(Class)modelClass{
    if (!modelClass) {
        return nil;
    }
    return [self.dbHelper searchWithSQL:@"select * from @t" toClass:modelClass];
}


#pragma mark - set/get

- (LKDBHelper *)dbHelper
{
    if (!_dbHelper)
    {
        _dbHelper = [[LKDBHelper alloc] initWithDBName:ST_MAINDB_NAME];
        //_dbHelper = [[LKDBHelper alloc] initWithDBPath:self.dbFilePath];
        //[_dbHelper dropAllTable];
    }
    return _dbHelper;
}

- (NSString *)dbFilePath
{
    if (!_dbFilePath)
    {
        // document目录下
        NSArray *documentArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
        NSString *document = [documentArray objectAtIndex:0];
        _dbFilePath = [document stringByAppendingPathComponent:ST_MAINDB_NAME];
    }
    NSLog(@"数据库路径: %@", _dbFilePath);
    return _dbFilePath;
}


@end
